<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户余额;" prop="urBalance">
      <el-input v-model="dataForm.urBalance" placeholder="用户余额"></el-input>
    </el-form-item>
    <el-form-item label="用户名（登录账户）   唯一" prop="urName">
      <el-input v-model="dataForm.urName" placeholder="用户名（登录账户）   唯一"></el-input>
    </el-form-item>
    <el-form-item label="电子邮箱（登录账户）" prop="urEmail">
      <el-input v-model="dataForm.urEmail" placeholder="电子邮箱（登录账户）"></el-input>
    </el-form-item>
    <el-form-item label="用户名密码" prop="urPassword">
      <el-input v-model="dataForm.urPassword" placeholder="用户名密码"></el-input>
    </el-form-item>
    <el-form-item label="状态是否激活 （0没激活，1激活）" prop="urStatus">
      <el-input v-model="dataForm.urStatus" placeholder="状态是否激活 （0没激活，1激活）"></el-input>
    </el-form-item>
    <el-form-item label="激活码" prop="urUuid">
      <el-input v-model="dataForm.urUuid" placeholder="激活码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          urId: 0,
          urBalance: '',
          urName: '',
          urEmail: '',
          urPassword: '',
          urStatus: '',
          urUuid: ''
        },
        dataRule: {
          urBalance: [
            { required: true, message: '用户余额;不能为空', trigger: 'blur' }
          ],
           urName: [
            { required: true, message: '用户名（登录账户）   唯一不能为空', trigger: 'blur' }
          ],
          urEmail: [
            { required: true, message: '电子邮箱（登录账户）不能为空', trigger: 'blur' }
          ],
          urPassword: [
            { required: true, message: '用户名密码不能为空', trigger: 'blur' }
          ],
          urStatus: [
            { required: true, message: '状态是否激活 （0没激活，1激活）不能为空', trigger: 'blur' }
          ],
          urUuid: [
            { required: true, message: '激活码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.urId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.urId) {
            this.$http({
              url: this.$http.adornUrl(`/mallgn/user/info/${this.dataForm.urId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.urBalance = data.user.urBalance
                this.dataForm. urName = data.user. urName
                this.dataForm.urEmail = data.user.urEmail
                this.dataForm.urPassword = data.user.urPassword
                this.dataForm.urStatus = data.user.urStatus
                this.dataForm.urUuid = data.user.urUuid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/mallgn/user/${!this.dataForm.urId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'urId': this.dataForm.urId || undefined,
                'urBalance': this.dataForm.urBalance,
                'urName': this.dataForm.urName,
                'urEmail': this.dataForm.urEmail,
                'urPassword': this.dataForm.urPassword,
                'urStatus': this.dataForm.urStatus,
                'urUuid': this.dataForm.urUuid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
